#include "input.h"

// Include libraries
#include "math.h"

Input::Input()
{
	// Initiate key holders
	keysHeld=0;
	keysPressed=0;
	keysReleased=0;
}

void Input::scanKeys()
{
	// Event
	SDL_Event event;

	// Reset pressed / released key holders
	keysPressed=0;
	keysReleased=0;

	// While there's an event to handle
	while (SDL_PollEvent(&event))
	{
		// If a key was pressed
		if(event.type==SDL_KEYDOWN)
		{
			// Set key flags appropriately
			switch(event.key.keysym.sym)
			{
				case SDLK_ESCAPE:
					keysPressed|=KEY_ESCAPE;
					keysHeld|=KEY_ESCAPE;
					break;
				case SDLK_LEFT:
					keysPressed|=KEY_LEFT;
					keysHeld|=KEY_LEFT;
					break;
				case SDLK_RIGHT:
					keysPressed|=KEY_RIGHT;
					keysHeld|=KEY_RIGHT;
					break;
				case SDLK_UP:
					keysPressed|=KEY_UP;
					keysHeld|=KEY_UP;
					break;
				case SDLK_DOWN:
					keysPressed|=KEY_DOWN;
					keysHeld|=KEY_DOWN;
					break;
				case SDLK_z:
					keysPressed|=KEY_FIRE;
					keysHeld|=KEY_FIRE;
					break;
				case SDLK_x:
					keysPressed|=KEY_SLIDE;
					keysHeld|=KEY_SLIDE;
					break;
				case SDLK_c:
					keysPressed|=KEY_JUMP;
					keysHeld|=KEY_JUMP;
					break;
				case SDLK_RETURN:
					keysPressed|=KEY_ACCEPT;
					keysHeld|=KEY_ACCEPT;
					break;
				default:
				break;
			}
		}

		// If a key was released
		if(event.type==SDL_KEYUP)
		{
			// Set key flags appropriately
			switch(event.key.keysym.sym)
			{
				case SDLK_ESCAPE:
					keysReleased|=KEY_ESCAPE;
					keysHeld&=~KEY_ESCAPE;
					break;
				case SDLK_LEFT:
					keysReleased|=KEY_LEFT;
					keysHeld&=~KEY_LEFT;
					break;
				case SDLK_RIGHT:
					keysReleased|=KEY_RIGHT;
					keysHeld&=~KEY_RIGHT;
					break;
				case SDLK_UP:
					keysReleased|=KEY_UP;
					keysHeld&=~KEY_UP;
					break;
				case SDLK_DOWN:
					keysReleased|=KEY_DOWN;
					keysHeld&=~KEY_DOWN;
					break;
				case SDLK_z:
					keysReleased|=KEY_FIRE;
					keysHeld&=~KEY_FIRE;
					break;
				case SDLK_x:
					keysReleased|=KEY_SLIDE;
					keysHeld&=~KEY_SLIDE;
					break;
				case SDLK_c:
					keysReleased|=KEY_JUMP;
					keysHeld&=~KEY_JUMP;
					break;
				case SDLK_RETURN:
					keysReleased|=KEY_ACCEPT;
					keysHeld&=~KEY_ACCEPT;
					break;
				default:
				break;
			}
		}

		//If the user has Xed out the window
		else if(event.type==SDL_QUIT)
		{
			// Set escape flag
			keysPressed|=KEY_ESCAPE;
			keysReleased|=KEY_ESCAPE;
		}
	}
/*
	// Update key holders
		// Reset values
		keysReleased=0;
		keysPressed=0;

		// Loop through all 32 bits
		for (u16 i=0; i<32; i++)
		{
			// If released
			if (!(tempKeys&((u32) pow(2, i)))&&(keysHeld&((u32) pow(2, i))))
			{
				// Set released
				keysReleased|=(u32) pow(2, i);
			}

			// If pressed
			if ((tempKeys&((u32) pow(2, i)))&&!(keysHeld&((u32) pow(2, i))))
			{
				// Set pressed
				keysReleased|=(u32) pow(2, i);
			}
		}

		// Set held
		keysHeld=tempKeys;
*/
}
